package com.ruoyi.my.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.WebTakeTime;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.my.domain.Shop;
import com.ruoyi.my.service.IshopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商家Controller
 *
 * @author liuxusheng
 * @date 2022-01-07
 */
@RestController
@RequestMapping("/my/shop")
public class ShopController extends BaseController {
    @Autowired
    private IshopService shopService;

    /**
     * 查询商家列表
     */
    @WebTakeTime
    @PreAuthorize("@ss.hasPermi('my:shop:list')")
    @GetMapping("/list")
    public TableDataInfo list(Shop shop) {
        logger.info("获取商家列表"+shop);
        startPage();
        List<Shop> list = shopService.selectshopList(shop);
        return getDataTable(list);
    }

    /**
     * 导出商家列表
     */
    @PreAuthorize("@ss.hasPermi('my:shop:export')")
    @Log(title = "商家", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Shop shop) {
        List<Shop> list = shopService.selectshopList(shop);
        ExcelUtil<Shop> util = new ExcelUtil<Shop>(Shop. class);
        util.exportExcel(response, list, "商家数据");
    }

    /**
     * 获取商家详细信息
     */
    @WebTakeTime
//    @PreAuthorize("@ss.hasPermi('my:shop:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(shopService.selectshopById(id));
    }

    /**
     * 新增商家
     */
    @WebTakeTime
    @PreAuthorize("@ss.hasPermi('my:shop:add')")
    @Log(title = "商家", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Shop shop) {
        return toAjax(shopService.insertshop(shop));
    }

    /**
     * 修改商家
     */
    @WebTakeTime
    @PreAuthorize("@ss.hasPermi('my:shop:edit')")
    @Log(title = "商家", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Shop shop) {
        return toAjax(shopService.updateshop(shop));
    }

    /**
     * 删除商家
     */
    @WebTakeTime
    @PreAuthorize("@ss.hasPermi('my:shop:remove')")
    @Log(title = "商家", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(shopService.deleteshopByIds(ids));
    }
}
